//
//  CustomShareView.m
//  Artv
//
//  Created by SC on 16/7/26.
//  Copyright © 2016年 SC. All rights reserved.
//

#import "CustomShareView.h"
#import "WXApi.h"
#import <UMSocialCore/UMSocialCore.h>   //友盟分享头文件

@interface CustomShareView ()
@property(nonatomic) BOOL isSend;
@property(nonatomic, strong) UILabel *titleLabel;
@end

@implementation CustomShareView
- (void)dealloc{
    
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, HEIGHT-64, WIDTH, 170);
        self.backgroundColor=[UIColor whiteColor];
        _mImageArr = [[NSMutableArray alloc] init];
        _colorArray = [[NSMutableArray alloc] init];
        _shareArr = [[NSMutableArray alloc] init];
        [self setUI];
    }
    return self;
}
- (instancetype)initSendView
{
    self = [super init];
    if (self) {
        self.isSend = YES;
        self.frame = CGRectMake(0, HEIGHT-64, WIDTH, 170);
        self.backgroundColor=[UIColor whiteColor];
        _mImageArr = [[NSMutableArray alloc] init];
        _colorArray = [[NSMutableArray alloc] init];
        _shareArr = [[NSMutableArray alloc] init];
        [self setUI];
    }
    return self;
}

#pragma mark - SET
//特定H5分享格式
- (void)setShareStyleType:(NSNumber *)shareStyleType
{
    if (shareStyleType.integerValue == 0) {//不改变
        
    }
    else {
        
        [self removeToolViews];
        [_mImageArr removeAllObjects];
        [_colorArray removeAllObjects];
        [_shareArr removeAllObjects];
        BOOL installtWX = [WXApi isWXAppInstalled];//微信
        BOOL installQQ = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];//QQ
        
        if (shareStyleType.integerValue & ShareStyleTypeWxFriend) {//朋友圈
            if (installtWX) {//安装了微信
                [_mImageArr addObject:[IconFont shareInstance].iconpengyouquan];
                [_colorArray addObject:HEXCOLOR(kColorGreen2, 1)];
                [_shareArr addObject:@"朋友圈"];
            }
        }
        
        if (shareStyleType.integerValue & ShareStyleTypeWx) {//微信
            if (installtWX) {//安装了微信
                [_mImageArr addObject:[IconFont shareInstance].iconweixin];
                [_colorArray addObject:HEXCOLOR(kColorGreen1, 1)];
                [_shareArr addObject:@"微信"];
            }
        }
        
        if (shareStyleType.integerValue & ShareStyleTypeQQ) {//QQ
            if ([_shareArr containsObject:@"朋友圈"] && ![_shareArr containsObject:@"微信"]) {
                if (installQQ) {//安装了QQ
                    [_mImageArr addObject:[IconFont shareInstance].iconqq];
                    [_colorArray addObject:HEXCOLOR(kColorBlue3, 1)];
                    [_shareArr addObject:@"QQ"];
                }
            }
            else if (![_shareArr containsObject:@"朋友圈"] && [_shareArr containsObject:@"微信"]) {
                if (installQQ) {//安装了QQ
                    [_mImageArr insertObject:[IconFont shareInstance].iconqq atIndex:0];
                    [_colorArray insertObject:HEXCOLOR(kColorBlue3, 1) atIndex:0];
                    [_shareArr insertObject:@"QQ" atIndex:0];
                }
            }
            else if ([_shareArr containsObject:@"朋友圈"] && [_shareArr containsObject:@"微信"]) {
                if (installQQ) {//安装了QQ
                    [_mImageArr insertObject:[IconFont shareInstance].iconqq atIndex:1];
                    [_colorArray insertObject:HEXCOLOR(kColorBlue3, 1) atIndex:1];
                    [_shareArr insertObject:@"QQ" atIndex:1];
                }
            }
            else if (![_shareArr containsObject:@"朋友圈"] && ![_shareArr containsObject:@"微信"]) {
                if (installQQ) {//安装了QQ
                    [_mImageArr addObject:[IconFont shareInstance].iconqq];
                    [_colorArray addObject:HEXCOLOR(kColorBlue3, 1)];
                    [_shareArr addObject:@"QQ"];
                }
            }
        }
        
        if (shareStyleType.integerValue & ShareStyleTypeSina) {//新浪
            
            [_mImageArr addObject:[IconFont shareInstance].iconweibo];
            [_colorArray addObject:HEXCOLOR(kColorOrange1, 1)];
            [_shareArr addObject:@"新浪微博"];
        }
        [self addToolViews];
        
    }
}

//百宝箱分享格式
- (void)setIsHidden:(BOOL)isHidden
{
    _suspensionImageView.hidden = isHidden;
    _titleLabel.hidden = isHidden;
    [self removeToolViews];
    [_mImageArr removeAllObjects];
    [_colorArray removeAllObjects];
    [_shareArr removeAllObjects];
    BOOL installtWX = [WXApi isWXAppInstalled];//微信
    BOOL installQQ = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];//QQ
    if (installtWX && installQQ) {//都安装
        [_mImageArr addObject:[IconFont shareInstance].iconqq];
        [_mImageArr addObject:[IconFont shareInstance].iconweixin];
        [_colorArray addObject:HEXCOLOR(kColorBlue3, 1)];
        [_colorArray addObject:HEXCOLOR(kColorGreen1, 1)];
        [_shareArr addObject:@"QQ"];
        [_shareArr addObject:@"微信"];
    }
    else if (installtWX && !installQQ) {//微信安装，QQ没安装
        [_mImageArr addObject:[IconFont shareInstance].iconweixin];
        [_colorArray addObject:HEXCOLOR(kColorGreen1, 1)];
        [_shareArr addObject:@"微信"];
    }
    else if (!installtWX && installQQ) {//微信没安装，QQ安装
        [_mImageArr addObject:[IconFont shareInstance].iconqq];
        [_colorArray addObject:HEXCOLOR(kColorBlue3, 1)];
        [_shareArr addObject:@"QQ"];
    }
    else {//都没安装
        
    }
    [self addToolViews];
}

#pragma mark - Event Respond

- (void)setUI
{
    _shareBGView=[[UIView alloc]init];
    _shareBGView.frame=CGRectMake(0,HEIGHT, WIDTH, HEIGHT);
    _shareBGView.backgroundColor=[UIColor blackColor];
    _shareBGView.alpha=0.5;
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shareBGRemoveAction)];
    [_shareBGView addGestureRecognizer:tap];
    
    _suspensionImageView = [[UIImageView alloc] init];
    _suspensionImageView.frame = CGRectMake(30, -90, WIDTH-60, 90);
    _suspensionImageView.image = [UIImage imageNamed:@"底部分享送礼悬浮图片"];
    _suspensionImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_suspensionImageView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    _titleLabel.text = @"一键分享即获100学分";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:12];
    _titleLabel.textColor = HEXCOLOR(kColorBlue2, 1);
    [self addSubview:_titleLabel];

    [self setShareBtn];
    
    
    
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 89+30, WIDTH, 1)];
    lineView.backgroundColor=[UIColor colorWithRed:0.94 green:0.93 blue:0.96 alpha:1];
    [self addSubview:lineView];
    
    UIButton *cancelbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    cancelbtn.frame=CGRectMake(10, 95+30, WIDTH - 20, 40);
    [cancelbtn addTarget:self action:@selector(shareRemoveAction) forControlEvents:UIControlEventTouchUpInside];
    cancelbtn.backgroundColor = [UIColor whiteColor];
    [cancelbtn setTitle:@"待会儿再说" forState:UIControlStateNormal];
    [cancelbtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    [self addSubview:cancelbtn];
}

- (void)setShareBtn
{
    BOOL installtWX = [WXApi isWXAppInstalled];//微信
    BOOL installQQ = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];//QQ
    
    if (installtWX && installQQ) {//都安装
        NSArray *mArray = @[[IconFont shareInstance].iconpengyouquan,
                       [IconFont shareInstance].iconqq,
                       [IconFont shareInstance].iconweixin,
                       [IconFont shareInstance].iconweibo];
        NSArray *cArray = @[HEXCOLOR(kColorGreen2, 1),HEXCOLOR(kColorBlue3, 1),HEXCOLOR(kColorGreen1, 1),HEXCOLOR(kColorOrange1, 1)];
        NSArray *sArray = @[@"朋友圈",@"QQ",@"微信",@"新浪微博"];
        [_mImageArr addObjectsFromArray:mArray];
        [_colorArray addObjectsFromArray:cArray];
        [_shareArr addObjectsFromArray:sArray];
    }
    else if (installtWX && !installQQ) {//微信安装，QQ没安装
        NSArray *mArray = @[[IconFont shareInstance].iconpengyouquan,
                       [IconFont shareInstance].iconweixin,
                       [IconFont shareInstance].iconweibo];
        NSArray *cArray = @[HEXCOLOR(kColorGreen2, 1),HEXCOLOR(kColorGreen1, 1),HEXCOLOR(kColorOrange1, 1)];
        NSArray *sArray = @[@"朋友圈",@"微信",@"新浪微博"];
        [_mImageArr addObjectsFromArray:mArray];
        [_colorArray addObjectsFromArray:cArray];
        [_shareArr addObjectsFromArray:sArray];
    }
    else if (!installtWX && installQQ) {//微信没安装，QQ安装
        NSArray *mArray = @[[IconFont shareInstance].iconqq,
                       [IconFont shareInstance].iconweibo];
        NSArray *cArray = @[HEXCOLOR(kColorBlue3, 1),HEXCOLOR(kColorOrange1, 1)];
        NSArray *sArray = @[@"QQ",@"新浪微博"];
        [_mImageArr addObjectsFromArray:mArray];
        [_colorArray addObjectsFromArray:cArray];
        [_shareArr addObjectsFromArray:sArray];
    }
    else {//都没安装
        
        [_mImageArr addObject:[IconFont shareInstance].iconweibo];
        [_colorArray addObject:HEXCOLOR(kColorOrange1, 1)];
        [_shareArr addObject:@"新浪微博"];
    }
    [self addToolViews];
    
    
    
}

- (void)addToolViews
{
    for (int i=0; i<_shareArr.count; i++)
    {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 34, 60, 85)];
        CGPoint center = view.center;
        center.x = (WIDTH/(_shareArr.count *2)) * (2*i +1);
        view.center = center;
        view.tag = 50+i;
        view.backgroundColor = [UIColor clearColor];
        [self addSubview:view];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(5, 5, 50, 50);
        shareBtn.titleLabel.font = [UIFont fontWithName:@"iconfont" size:50];
        [shareBtn setTitle:_mImageArr[i] forState:UIControlStateNormal];
        [shareBtn setTitleColor:_colorArray[i] forState:UIControlStateNormal];
        shareBtn.tag = 100+i;
        [shareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:shareBtn];
        
        UILabel *label=[[UILabel alloc]init];
        label.text=_shareArr[i];
        label.font=[UIFont systemFontOfSize:13];
        [label sizeToFit];
        label.tag = 200+i;
        label.frame=CGRectMake(-5, 57, 70, 25);
        label.textAlignment=NSTextAlignmentCenter;
        label.textColor=[UIColor darkGrayColor];
        [view addSubview:label];
    }
}

- (void)removeToolViews
{
    for (int i=0; i<_shareArr.count; i++) {
        UIView *view = [self viewWithTag:50+i];
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        UILabel *label = [self viewWithTag:200+i];
        [view removeFromSuperview];
        [btn removeFromSuperview];
        [label removeFromSuperview];
        view = nil;
        btn = nil;
        label = nil;
    }
}

#pragma mark - Action

//分享方法
- (void)shareAction:(UIButton*)btn
{
    if ([_delegate respondsToSelector:@selector(shareDelegateMethod:count:)]) {
        if (self.isSend) {
            if ([_shareArr[btn.tag-100] isEqualToString:@"QQ"]) {
                [_delegate shareDelegateMethod:0 count:_shareArr.count];
            }else{
                [_delegate shareDelegateMethod:1 count:_shareArr.count];
            }
            
            return;
        }
        [_delegate shareDelegateMethod:btn.tag count:_shareArr.count];
    }
}

//取消分享
- (void)shareRemoveAction
{
    if ([_delegate respondsToSelector:@selector(delegateRemoveShareView)]) {
        [_delegate delegateRemoveShareView];
    }
}

//取消背景图
- (void)shareBGRemoveAction
{
    if ([_delegate respondsToSelector:@selector(delegateRemoveShareBGView)]) {
        [_delegate delegateRemoveShareBGView];
    }
}

- (void)liveSetting
{
    self.titleLabel.text = @"一键分享即获200学分";
}

@end
